#include<iostream>
using namespace std;
 #include <sys/types.h>          /* See NOTES */
 #include <sys/socket.h>
#include<unistd.h>
#include<cstring>
#include<arpa/inet.h>
#define PORT 9090
#define BUFFER_SIZE 64
#include<json-c/json.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// struct Msg
// {
//      int m_type;
//     char m_name[BUFFER_SIZE];
//     char m_passwd[BUFFER_SIZE];
//     /*搜索歌名*/
//     char searchSongName[BUFFER_SIZE];
//     /*搜索歌手*/
//     char searchSinger[BUFFER_SIZE];
//     /*喜欢的歌曲*/
//     char loveSongName[BUFFER_SIZE];
//     /*歌曲风格*/
//     char songStyle[BUFFER_SIZE];

// };

// void menu()
// {
//    // system("clear ");
//     cout<<"1.注册"<<endl;
//     cout<<"2.登录"<<endl;
//     cout<<"3.退出"<<endl;
// }

// void registerFuc(int sockfd)
// {
//         char username[BUFFER_SIZE];
//         cout<<"请输入用户名：";
//         scanf("%s",username);
        
//         char passwd[BUFFER_SIZE];
//         cout<<"请输入密码：";
//         scanf("%s",passwd);
//         /*创建json对象*/
//         json_object *jsonObj=json_object_new_object(); 
//         json_object_object_add(jsonObj,"type",json_object_new_int(REGISTER));
//         json_object_object_add(jsonObj,"username",json_object_new_string(username));
//         json_object_object_add(jsonObj,"passwd",json_object_new_string(passwd));

//         /*将json对象转换成字符串*/
//         const char *jsonStr=json_object_to_json_string(jsonObj);

//         // Msg msg;

//         // /*发送信息到服务器*/

//          write(sockfd,jsonStr,strlen(jsonStr));

// }
// void loginFunc(int sockfd)
// {
//      char username[BUFFER_SIZE];
//         cout<<"请输入用户名：";
//         scanf("%s",username);
        
//         char passwd[BUFFER_SIZE];
//         cout<<"请输入密码：";
//         scanf("%s",passwd);

//         /*创建json对象*/
//         json_object *jsonObj=json_object_new_object(); 
//         json_object_object_add(jsonObj,"type",json_object_new_int(LOGIN));
//         json_object_object_add(jsonObj,"username",json_object_new_string(username));
//         json_object_object_add(jsonObj,"passwd",json_object_new_string(passwd));

//         /*将json对象转换成字符串*/
//         const char *jsonStr=json_object_to_json_string(jsonObj);


//     //    Msg user(LOGIN,username,passwd);

//         /*发送信息到服务器*/
//         int length=strlen(jsonStr);
//         write(sockfd,&length,sizeof(int));

//          write(sockfd,jsonStr,strlen(jsonStr));

    
// }


struct SendMe {
    int num;
    char buffer[1024];
};

int main() {
    /* 创建通信文件描述符 */
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == -1) {
        perror("create error:");
        exit(1);
    }

    /* 链接服务器 */
    sockaddr_in serverAddress;
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(PORT);
    const char *serverIp = "192.168.111.128";
    inet_pton(AF_INET, serverIp, &(serverAddress.sin_addr.s_addr));

    /* 客户端发送信息给服务器 */
    SendMe t1;
    t1.num = 1;
    cout << "请输入文件名:" << endl;
    cin.getline(t1.buffer, sizeof(t1.buffer));  // 防止缓冲区溢出

    // 发送文件名和标识
    sendto(sockfd, &t1.num, sizeof(int), 0, (const sockaddr*)&serverAddress, sizeof(serverAddress));
    sendto(sockfd, t1.buffer, strlen(t1.buffer), 0, (const sockaddr*)&serverAddress, sizeof(serverAddress));

    // 打开文件
    int fd = open(t1.buffer, O_RDONLY);
    if (fd == -1) {
        perror("open error:");
        exit(1);
    }

    /* 清除脏数据 */
    memset(t1.buffer, 0, sizeof(t1.buffer));
    int requestBytes = sizeof(t1.buffer) - 1;

    // 发送文件内容
    while (1) {
        int readBytes = read(fd, t1.buffer, requestBytes);
        if (readBytes == 0) {
            // 发送结束标志
            char endMarker[] = "EOF";
            sendto(sockfd, endMarker, strlen(endMarker), 0, (const sockaddr*)&serverAddress, sizeof(serverAddress));
            break;
        } else {
            sendto(sockfd, &t1.num, sizeof(int), 0, (const sockaddr*)&serverAddress, sizeof(serverAddress));
            sendto(sockfd, t1.buffer, readBytes, 0, (const sockaddr*)&serverAddress, sizeof(serverAddress));
            t1.num++;
        }
        memset(t1.buffer, 0, sizeof(t1.buffer));  // 清除缓冲区
    }

    // 关闭文件描述符
    close(fd);

    // 关闭套接字
    close(sockfd);
    return 0;
}
        
        

    
    
        //读取直到换行符的所有字符，包含空格
        //使用fgets 读取一整行，包含空格
        // fgets(str,sizeof(str),stdin);
        // write(sockfd,data,strlen(data));
    
    
    // /*清除脏数据*/
    // memset(data,0,sizeof(data));
    // /*接受服务器发送回的信息*/
    // read(sockfd,data,sizeof(data));

    // cout<<"服务器发送过来的数据是："<<data<<endl;














